package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    //http://localhost:8080/goods/listInventory
    @PostMapping("listInventory")
    public Map<String,Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId){
        page=(page-1)*rows;
        Map<String, Object> map = new HashMap<>();
        List<Goods> goodsList = goodsService.getGoods(page,rows,codeOrName,goodsTypeId);
        map.put("rows",goodsList);
        map.put("total", goodsList.size());
        return map;
    }

    /*
    请求URL：http://localhost:8080/goods/list
    请求参数：Integer page, Integer rows, String goodsName, Integer goodsTypeId
    请求方式：POST
    返回值类型：JSON
    返回值：Map<String,Object>
     */
    //商品管理页面详情显示
    @PostMapping("list")
    public Map<String,Object> list(Integer page, Integer rows,  String goodsName, Integer goodsTypeId){
        page=(page-1)*rows;
        Map<String, Object> map = new HashMap<>();
        List<Goods> goods = goodsService.getGoods(page, rows, goodsName, goodsTypeId);
        map.put("total",goods.size());
        map.put("rows",goods);
        return map;
    }
    /*
    请求URL：http://localhost:8080/goods/save?goodsId=37
    请求参数：Goods goods
    请求方式：POST
    返回值类型：JSON
    返回值：ServiceVO
     */
    @PostMapping("save")
    public ServiceVO save(Goods goods){
        goodsService.addOrUpdate(goods);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    /*
    请求URL：http://localhost:8080/goods/delete
    请求参数：Integer goodsId
    请求方式：POST
    返回值类型：JSON
    返回值：ServiceVO
     */
    @PostMapping("delete")
    public ServiceVO delete(Integer goodsId){
        return goodsService.deleteById(goodsId);
    }


    /*
    请求URL：http://localhost:8080/goods/getNoInventoryQuantity
    请求参数：Integer page,Integer rows,String nameOrCode
    请求方式：POST
    返回值类型：JSON
    返回值：Map<String,Object>
     */
    //无库存商品列表展示（可以根据商品名称或编码查询）
    @PostMapping("getNoInventoryQuantity")
    public Map<String,Object> getNoInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        Map<String, Object> map = new HashMap<>();
        List<Goods> list=goodsService.getNoInventoryQuantity(page,rows,nameOrCode);
        map.put("toral",list.size());
        map.put("rows",list);
        return map;
    }

    /*
  请求URL：http://localhost:8080/goods/getHasInventoryQuantity
  请求参数：Integer page,Integer rows,String nameOrCode
  请求方式：POST
  返回值类型：JSON
  返回值：Map<String,Object>
   */
    //有库存商品列表展示（可以根据商品名称或编码查询）
    @PostMapping("getHasInventoryQuantity")
    public Map<String,Object> getHasInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        Map<String, Object> map = new HashMap<>();
        List<Goods> list=goodsService.getHasInventoryQuantity(page,rows,nameOrCode);
        map.put("toral",list.size());
        map.put("rows",list);
        return map;
    }

    /*
    请求URL：http://localhost:8080/goods/saveStock?goodsId=25
    请求参数：Integer goodsId,Integer inventoryQuantity,double purchasingPrice
    请求方式：POST
    返回值类型：JSON
    返回值：ServiceVO
     */
    //添加库存、修改数量或成本价-->都是修改操作
    @PostMapping("saveStock")
    public ServiceVO saveStock(Integer goodsId,Integer inventoryQuantity,double purchasingPrice){
        return goodsService.updateStock(goodsId,inventoryQuantity,purchasingPrice);
    }

    /*
    请求URL：http://localhost:8080/goods/deleteStock
    请求参数：Integer goodsId
    请求方式：POST
    返回值类型：JSON
    返回值：ServiceVO
     */
    @PostMapping("deleteStock")
    public ServiceVO deleteStock(Integer goodsId){
        return goodsService.deleteStock(goodsId);
    }

    /*
    请求URL：http://localhost:8080/goods/listAlarm
    请求参数：无
    请求方式：POST
    返回值类型：JSON
    返回值：Map<String,Object>
     */

    //查询所有当前库存量小于库存下限的商品信息
    @PostMapping("listAlarm")
    public Map<String,Object> listAlarm(){
        Map<String, Object> map = new HashMap<>();
        List<Goods> list=goodsService.selectListAlarm();
        map.put("rows",list);
        return map;
    }
}
